//package src.scorpionhermit;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class FiveInARow extends JFrame implements MouseListener {
	Vector v = new Vector();//记录黑棋，白棋状态
	Vector white = new Vector();//记录白棋状态
	Vector black = new Vector();//记录黑棋状态
	boolean b;
	int whiteCount, blackCount; //黑棋，白棋数量
	int w = 25;
	int px = 100, py = 100;
	int pxw = px + w, pyw = py + w;
	int width = w * 16, height = w * 16;
	int vline = width + px;
	int hline = height + py;

	//构造菜单方法
	public FiveInARow() {
		super("五子棋");
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		Container con = this.getContentPane();
		con.setLayout(new BorderLayout());
		this.addMouseListener(this);//添加鼠标监听事件
		this.setSize(600, 600);//设置窗口大小
		this.setBackground(Color.orange);//背景颜色
		this.setVisible(true);//可放大
	}

	public void paint(Graphics g) {
		g.clearRect(0, 0, this.getWidth(), this.getHeight());
		g.setColor(Color.BLACK);
		g.drawRect(px, py, width, height);//画点
		g.drawString("左键下棋，右键悔棋，最多撤销3次", 180, 70);

		for (int i=0; i<15; i++) {
			g.drawLine(pxw+i*w, py, pxw+i*w, hline);//绘制棋盘
			g.drawLine(px, pyw+i*w, vline, pyw+i*w);
		}

		for (int x=0; x<v.size(); x++) {
			String str = (String)v.get(x);
			String tmp[] = str.split("-");
			int a = Integer.parseInt(tmp[0]);
			int b = Integer.parseInt(tmp[1]);
			a = a * w + px;
			b = b * w + py;
			if (x%2 == 0) {
				g.setColor(Color.WHITE);
			} else {
				g.setColor(Color.BLACK);
			}
			g.fillArc(a-w/2, b-w/2, w, w, 0, 360);
		}
	}

	@Override
	public void mouseClicked(MouseEvent e) {//鼠标单击事件
		if (e.getButton() == e.BUTTON1) {//左键下棋
			int x = e.getX();
			int y = e.getY();
			x = (x - x % w) + (x % w > w / 2 ? w : 0);
			y = (y - y % w) + (y % w > w / 2 ? w : 0);
			x = (x - px) / w;
			y = (y - py) / w;

			if (x >= 0 && y >= 0 && x <= 16 && y <= 16) {
				if (v.contains(x+"-"+y)) {
					JOptionPane.showMessageDialog(null, "不能下在同一个地方！", "警告", JOptionPane.WARNING_MESSAGE);
				} else {
					v.add(x+"-"+y);
					this.repaint();
					if (v.size() % 2 == 0) {
						black.add(x+"-"+y);

						this.victory(x, y, black);

					} else {
						white.add(x+"-"+y);
						this.victory(x, y, white);
//
					}

				}
			} else {

			}
		}

		if (e.getButton() == e.BUTTON3) {	//右键悔棋
			if (v.isEmpty()) {
				JOptionPane.showMessageDialog(this, "棋盘上没有棋子了");
			} else {
				if (v.size() % 2 == 0) {	//黑棋悔棋数量
					blackCount++;
					if (blackCount > 3) {
						JOptionPane.showMessageDialog(this, "黑棋悔棋不能超过3次");
					} else {
						v.remove(v.lastElement());
						black.remove(black.lastElement());
						this.repaint();
					}
				} else {//白棋悔棋数量
					whiteCount++;
					if (whiteCount > 3) {
						JOptionPane.showMessageDialog(this, "白棋悔棋不能超过3次");
					} else {
						v.remove(v.lastElement());
						white.remove(white.lastElement());
						this.repaint();
					}
				}
			}
		}
	}


	private void victory(int x, int y, Vector contain) {
		int cv = 0;	//横向棋子数量
		int ch = 0;	//纵向棋子数量
		int ci1 = 0; //左斜向棋子数量
		int ci2 = 0; //右斜向棋子数量

		for (int i=1; i<5; i++) {
			if (contain.contains((x+i)+"-"+y)) {
				ch++;
			} else {
				break;
			}
		}
		for (int i=1; i<5; i++) {
			if (contain.contains((x-i)+"-"+y)) {
				ch++;
			} else {
				break;
			}
		}


		for (int i=1; i<5; i++) {
			if (contain.contains(x+"-"+(y+i))) {
				cv++;
			} else {
				break;
			}
		}
		for (int i=1; i<5; i++) {
			if (contain.contains(x+"-"+(y-i))) {
				cv++;
			} else {
				break;
			}
		}

		for (int i=1; i<5; i++) {
			if (contain.contains((x+i)+"-"+(y+i))) {
				ci1++;
			} else {
				break;
			}
		}
		for (int i=1; i<5; i++) {
			if (contain.contains((x-i)+"-"+(y-i))) {
				ci1++;
			} else {
				break;
			}
		}


		for (int i=1; i<5; i++) {
			if (contain.contains((x+i)+"-"+(y-i))) {
				ci2++;
			} else {
				break;
			}
		}
		for (int i=1; i<5; i++) {
			if (contain.contains((x-i)+"-"+(y+i))) {
				ci2++;
			} else {
				break;
			}
		}
		//有任意方向达到五子，即判定输赢
		if (ch>=4 || cv>=4 ||ci1>=4 ||ci2>=4) {
			System.out.println(v.size()+"棋");
			if (v.size() % 2 == 0) {
				JOptionPane.showMessageDialog(null, "黑棋获胜");
				blackCount=0;
				whiteCount=0;
			} else {
				JOptionPane.showMessageDialog(null, "白棋获胜");
				blackCount=0;
				whiteCount=0;
			}

			//清屏操作
			this.v.clear();
			this.black.clear();
			this.white.clear();
			this.repaint();
		}
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub

	}

}
